﻿using System;
using System.Collections.Generic;
using System.Text;
using DataTransferObject;
namespace BusinessLogicLayer
{
    public class SoSanh 
    {
        /*
         *  SoSanh()>>> Contructor
         *  public enum CauHoiCompareType>>> Các Column cho phép Sort của Câu Hỏi
         *  public enum BoDeCompareType>>> Các Column cho phép Sort của Bộ Đề
         *  public enum BaiVietCompareType>>> Các Column cho phép Sort của Bài Viết
         *  public enum ThanhVienCompareType>>> Các Column cho phép Sort của Thành Viên
         *  public enum BinhLuanCompareType>>> Các Column cho phép Sort của Bình Luận
         *  public enum TheLoaiCompareType>>> Các Column cho phép Sort của thể loại
         *  public enum ChuyenMucCompareType >>> Các Column cho phép Sort của chuyên mục
         *  public enum TypeSort >>> Loại sắp xếp : Tăng hoặc giảm
         *  public enum TableName >>> Danh sách các bảng dữ liệu
         *  Property
         *  public int CompareMethod(object oX, object oY)>>> So sánh 2 Object
         * */
        public SoSanh()
        {
            _typeSort = TypeSort.Giam;
        }

        public enum CauHoiCompareType
        {
            MaCauHoi,
            NoiDung,
            ChuyenMuc,
            KyNang,
            MucDo
        }

        public enum BoDeCompareType
        {
            MaBoDe,
            TenBoDe,
            ChuyenMuc,
            SoCau,
            ThoiGian,
            SoLanThucHien
        }

        public enum BaiVietCompareType
        {
            MaBaiViet,
            TieuDe,
            TheLoai,
            NgayKhoiTao,
            TenThanhVien
        }

        public enum ThanhVienCompareType
        {
            MaThanhVien,
            TenThanhVien,
            Email,
            SoBaiViet
        }

        public enum BinhLuanCompareType
        {
            MaBinhLuan,
            NoiDung,
            TacGia
        }

        public enum TheLoaiCompareType
        {
            MaTheLoai,
            TenTheLoai
        }

        public enum ChuyenMucCompareType
        {
            MaChuyenMuc,
            TenChuyenMuc
        }

        public enum TypeSort
        {
            Tang,
            Giam
        }

        public enum TableName
        {
            CauHoi,
            BoDe,
            BaiViet,
            BinhLuan,
            TheLoai,
            ChuyenMuc,
            ThanhVien
        }

        private CauHoiCompareType _cauhoiCompareType;
        private BoDeCompareType _bodeCompareType;
        private BaiVietCompareType _baivietCompareType;
        private BinhLuanCompareType _binhluanCompareType;
        private TheLoaiCompareType _theloaiCompareType;
        private ChuyenMucCompareType _chuyenmucCompareType;
        private ThanhVienCompareType _thanhvienCompareType;
        private TableName _tableName;
        private TypeSort _typeSort;
       
        public CauHoiCompareType CauHoiCompareTypeMethod
        {
            get
            {
                return _cauhoiCompareType;
            }
            set
            {
                _cauhoiCompareType = value;
            }
        }

        public BoDeCompareType BoDeCompareTypeMethod
        {
            get
            {
                return _bodeCompareType;
            }
            set
            {
                _bodeCompareType = value;
            }
        }

        public BaiVietCompareType BaiVietCompareTypeMethod
        {
            get
            {
                return _baivietCompareType;
            }
            set
            {
                _baivietCompareType = value;
            }
        }

        public BinhLuanCompareType BinhLuanCompareTypeMethod
        {
            get
            {
                return _binhluanCompareType;
            }
            set
            {
                _binhluanCompareType = value;
            }
        }

        public TheLoaiCompareType TheLoaiCompareTypeMethod
        {
            get
            {
                return _theloaiCompareType;
            }
            set
            {
                _theloaiCompareType = value;
            }
        }

        public ChuyenMucCompareType ChuyenMucCompareTypeMethod
        {
            get
            {
                return _chuyenmucCompareType;
            }
            set
            {
                _chuyenmucCompareType = value;
            }
        }

        public ThanhVienCompareType ThanhVienCompareTypeMethod
        {
            get
            {
                return _thanhvienCompareType;
            }
            set
            {
                _thanhvienCompareType = value;
            }
        }

        public TableName CompareTableNameMethod
        {
            get
            {
                return _tableName;
            }
            set
            {
                _tableName = value;
            }
        }

        public  TypeSort TypeSortMethod
        {
            get
            {
                return _typeSort;
            }
            set
            {
                _typeSort = value;
            }
        }

        public int CompareMethod(object oX, object oY)
        {
            int result = 0;
            if (_tableName == TableName.CauHoi)
            {
                clsCauHoi ch1;
                clsCauHoi ch2;
                if (oX is clsCauHoi)
                    ch1 = oX as clsCauHoi;
                else
                    throw new BusinessException("Input error.");
                if (oY is clsCauHoi)
                    ch2 = oY as clsCauHoi;
                else
                    throw new BusinessException("Input error.");
                switch (_cauhoiCompareType)
                {
                    case CauHoiCompareType.MaCauHoi:
                        result = ch1.MaCauHoi.CompareTo(ch2.MaCauHoi);
                        break;
                    case CauHoiCompareType.ChuyenMuc:
                        result = ch1.ChuyenMuc.TenChuyenMuc.CompareTo(ch2.ChuyenMuc.TenChuyenMuc);
                        break;
                    case CauHoiCompareType.KyNang:
                        result = ch1.KyNang.TenKyNang.CompareTo(ch2.KyNang.TenKyNang);
                        break;
                    case CauHoiCompareType.NoiDung:
                        result = ch1.NoiDung.CompareTo(ch2.NoiDung);
                        break;
                    default:
                        result = ch1.MucDo.TenMucDo.CompareTo(ch2.MucDo.TenMucDo);
                        break;
                }
               
            }
            else if (_tableName == TableName.BoDe)
            {
                clsBoDe bd1;
                clsBoDe bd2;
                if (oX is clsBoDe)
                    bd1 = oX as clsBoDe;
                else
                    throw new BusinessException("Input error.");
                if (oY is clsBoDe)
                    bd2 = oY as clsBoDe;
                else
                    throw new BusinessException("Input error.");
                switch (_bodeCompareType)
                {
                    case BoDeCompareType.MaBoDe:
                        result = bd1.MaDe.CompareTo(bd2.MaDe);
                        break;
                    case BoDeCompareType.ChuyenMuc:
                        result = bd1.ChuyenMuc.TenChuyenMuc.CompareTo(bd2.ChuyenMuc.TenChuyenMuc);
                        break;
                    case BoDeCompareType.SoCau:
                        result = bd1.SoCauHoi.CompareTo(bd2.SoCauHoi);
                        break;
                    case BoDeCompareType.SoLanThucHien:
                        result = bd1.SoLuotThucHien.CompareTo(bd2.SoLuotThucHien);
                        break;
                    case BoDeCompareType.TenBoDe:
                        result = bd1.TenDe.CompareTo(bd2.TenDe);
                        break;
                    default:
                        result = bd1.ThoiGianLamBai.CompareTo(bd2.ThoiGianLamBai);
                        break;
                }
            }
            else if (_tableName == TableName.BaiViet)
            {
                clsBaiViet bv1;
                clsBaiViet bv2;
                if (oX is clsBaiViet)
                    bv1 = oX as clsBaiViet;
                else
                    throw new BusinessException("Input error.");
                if (oY is clsBaiViet)
                    bv2 = oY as clsBaiViet;
                else
                    throw new BusinessException("Input error.");
                switch (_baivietCompareType)
                {
                    case BaiVietCompareType.MaBaiViet:
                        result = bv1.MaBaiViet.CompareTo(bv2.MaBaiViet);
                        break;
                    case BaiVietCompareType.NgayKhoiTao:
                        result = bv1.NgayKhoiTao.CompareTo(bv2.NgayKhoiTao);
                        break;
                    case BaiVietCompareType.TenThanhVien:
                        result = bv1.ThanhVien.TenThanhVien.CompareTo(bv2.ThanhVien.TenThanhVien);
                        break;
                    case BaiVietCompareType.TheLoai:
                        result = bv1.TheLoai.TenTheLoai.CompareTo(bv2.TheLoai.TenTheLoai);
                        break;
                    default:
                        result = bv1.TieuDe.CompareTo(bv2.TieuDe);
                        break;
                }
            }
            else if (_tableName == TableName.BinhLuan)
            {
                clsBinhLuan bl1;
                clsBinhLuan bl2;
                if (oX is clsBinhLuan)
                    bl1 = oX as clsBinhLuan;
                else
                    throw new BusinessException("Input error.");
                if (oY is clsBinhLuan)
                    bl2 = oY as clsBinhLuan;
                else
                    throw new BusinessException("Input error.");
                switch (_binhluanCompareType)
                {
                    case BinhLuanCompareType.MaBinhLuan:
                        result = bl1.MaBinhLuan.CompareTo(bl2.MaBinhLuan);
                        break;
                    case BinhLuanCompareType.NoiDung:
                        result = (bl1.NoiDung + bl1.NgayKhoiTao).CompareTo((bl2.NoiDung + bl2.NgayKhoiTao));
                        break;
                    default:
                        result = (bl1.HoTen + bl1.Email).CompareTo((bl2.HoTen + bl2.Email));
                        break;
                }
            }
            else if (_tableName == TableName.TheLoai)
            {
                clsTheLoai tl1;
                clsTheLoai tl2;
                if (oX is clsTheLoai)
                    tl1 = oX as clsTheLoai;
                else
                    throw new BusinessException("Input error.");
                if (oY is clsTheLoai)
                    tl2 = oY as clsTheLoai;
                else
                    throw new BusinessException("Input error.");
                switch (_theloaiCompareType)
                {
                    case TheLoaiCompareType.MaTheLoai:
                        result = tl1.MaTheLoai.CompareTo(tl2.MaTheLoai);
                        break;
                    default:
                        result = tl1.TenTheLoai.CompareTo(tl2.TenTheLoai);
                        break;
                }
            }
            else if (_tableName == TableName.ChuyenMuc)
            {
                clsChuyenMuc cm1;
                clsChuyenMuc cm2;
                if (oX is clsChuyenMuc)
                    cm1 = oX as clsChuyenMuc;
                else
                    throw new BusinessException("Input error.");
                if (oY is clsChuyenMuc)
                    cm2 = oY as clsChuyenMuc;
                else
                    throw new BusinessException("Input error.");
                switch (_chuyenmucCompareType)
                {
                    case ChuyenMucCompareType.MaChuyenMuc:
                        result = cm1.MaChuyenMuc.CompareTo(cm2.MaChuyenMuc);
                        break;
                    default:
                        result = cm1.TenChuyenMuc.CompareTo(cm2.TenChuyenMuc);
                        break;
                }
            }
            else if (_tableName == TableName.ThanhVien)
            {
                clsThanhVien tv1;
                clsThanhVien tv2;
                if (oX is clsThanhVien)
                    tv1 = oX as clsThanhVien;
                else
                    throw new BusinessException("Input error.");
                if (oY is clsThanhVien)
                    tv2 = oY as clsThanhVien;
                else
                    throw new BusinessException("Input error.");
                switch (_thanhvienCompareType)
                {
                    case ThanhVienCompareType.Email:
                        result = tv1.Email.CompareTo(tv2.Email);
                        break;
                    case ThanhVienCompareType.MaThanhVien:
                        result = tv1.MaThanhVien.CompareTo(tv2.MaThanhVien);
                        break;
                    case ThanhVienCompareType.TenThanhVien:
                        result = tv1.TenThanhVien.CompareTo(tv2.TenThanhVien);
                        break;
                    default :
                        result = tv1.SoBaiViet.CompareTo(tv2.SoBaiViet);
                        break;
                }
            }
            if (_typeSort == TypeSort.Giam)
                return -result;
            return result;
        }
    }
}
